package de.davvid.contracts;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.lcdui.*;

import de.davvid.core.TimeRecordsMIDlet;

public class DeleteContractMenu extends List implements CommandListener {

	private final Command delete, back;
	private final Hashtable indexToContracts;
	private final TimeRecordsMIDlet midlet;

	public DeleteContractMenu(TimeRecordsMIDlet midlet) {
		super("Delete contract",List.IMPLICIT);
		this.midlet = midlet;

		// Create Commands
		back = new Command("Back",Command.BACK, 1);
		delete = new Command("Delete",Command.OK, 0);
		addCommand(back);
		setSelectCommand(delete);
		setCommandListener(this);
		
		int i=midlet.contractDB.size();
		indexToContracts = new Hashtable();
		for(Enumeration e = midlet.contractDB.getContracts(); e.hasMoreElements();) {
			int index = append("Contract "+(i--),null);
			indexToContracts.put(new Integer(index), e.nextElement());
			
		}
	}

	public void commandAction(Command c, Displayable d) {
		if(c == delete) {
			midlet.contractDB.deleteContract(
					(Contract)indexToContracts.get(
							new Integer(getSelectedIndex())
					)
			);

		}
		midlet.showContractList();
	}

}
